package com.heima.define;

public class UserServiceImpl implements UserService{
    // 使用一个Map来模拟数据库
    private Map<Long, User> userMap = new HashMap<>();
    private Long idCounter = 1L;

    @Override
    public void createUser(User user) {
        user.setId(idCounter++);
        userMap.put(user.getId(), user);
    }

    @Override
    public User getUserById(Long id) {
        return userMap.get(id);
    }

    @Override
    public void updateUser(User user) {
        if (userMap.containsKey(user.getId())) {
            userMap.put(user.getId(), user);
        }
    }

    @Override
    public void deleteUser(Long id) {
        userMap.remove(id);
    }

    @Override
    public List<User> getAllUsers() {
        return new ArrayList<>(userMap.values());
    }
}
